package com.mcyy.financemanager.controller;

import com.mcyy.financemanager.entity.User;
import com.mcyy.financemanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@CrossOrigin
@RequestMapping("/financeManager")
public class FRcontroller {
    @Autowired
    private UserService userService;
    //用户登录并验证 返回用户id
    @PostMapping("tohome")
    @ResponseBody
    public String tohomeAPI(@RequestBody User user){
        //通过用户名 判断密码
        Integer a=userService.getuserbyloginname(user.getLoginName(),user.getPassword());
        System.out.println("登录");
        System.out.println(user);
        return a==0?"error":""+a;
    }
    //测试接口
    @GetMapping("test")
    @ResponseBody
    public User test(){
        userService.test();
        return null;
    }
    //注册用户
    @PostMapping("registeruser")
    public String registeruser(@RequestBody User user){
        int a=userService.adduser(user);
        return "你好注册";
    }
    //根据id获取用户信息
    @GetMapping("getuserinfobyid/{id}")
    public User getuserinfobyid(@PathVariable Integer id){
        User user=userService.getuserinfobyid(id);
        return user;
    }
    //超级管理员获取用户列表
    @GetMapping("getalluser")
    public List<User> getalluser(){
        List<User> users=userService.getalluser();
        return users;
    }
    //分页查询用户
    @GetMapping("getuserbypage/{limit}/{current}/{unamei}")
    public List<User> getuserbypage(@PathVariable Integer limit,@PathVariable Integer current,@PathVariable Integer unamei){
        Integer index=limit*(current-1);
        List<User> users=userService.getuserbypage(limit,index,unamei);
        return users;
    }
    //修改用户
    @PostMapping("updateuser")
    public String updateuser(@RequestBody User user){
        System.out.println(user);
        int a=userService.updateuser(user);
        return "success";
    }
    //通过用户id删除用户
    @GetMapping("deleteuserbyid/{id}")
    public String deleteuserbyid(@PathVariable Integer id){
        int a=userService.deleteuserbyid(id);
        return a!=0?"success":"error";
    }
}
